//
//  ThirdViewDetailViewController.m
//  TakeHer
//
//  Created by 胖疙瘩的小疙瘩 on 15/6/15.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "ThirdViewDetailViewController.h"
#import "DetailInfo.h"
#import "AFNetworking.h"
#import "MyCollectionReusableView.h"
#import "NotesDetailTableViewCell.h"
#import "UserViewController.h"
#import "TravelNoteModel.h"
#import "UIImageView+WebCache.h"
#import "NotesDetailTableViewCell.h"



@interface ThirdViewDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *notesTableView;
@property (nonatomic, retain) UIImageView *headerBackground;  //顶部图
@property (nonatomic, retain) UIImageView *authorImage;
@property (nonatomic, retain) TravelNoteModel *travelNotesDetail;
@property (nonatomic, retain) NSMutableDictionary *detailDic;

@property (nonatomic, retain) NSMutableArray *numberDayArr;  //第n天
@property (nonatomic, retain) NSString *numberDay;//第n天

//@property (nonatomic, retain) NSMutableArray *waypointsArr;   //路标
//@property (nonatomic, retain) NSMutableArray *daysArr;   //日期
//@property (nonatomic, retain) DataBaseHandler *dbHandler;  //


@end



@implementation ThirdViewDetailViewController






- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.detailDic = [NSMutableDictionary dictionary];
        //self.waypointsArr = [NSMutableArray array];
        //self.daysArr = [NSMutableArray array];
       
    }
    return self;
}

- (void)dealloc
{
    [_notesTableView release];
    [_headerBackground release];
    [_authorImage release];
    [_detail release];
    [_detailTitle release];
    [_urlId release];
    [_userImageString release];
    [_imageString release];
    
    
    [super dealloc];
}














- (void)dataHandle
{
    NSString *str = [NSString stringWithFormat:@"http://api.breadtrip.com/trips/%@/waypoints/?gallery_mode=1", self.urlId];
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"=======地址: %@", str);
    
    AFHTTPRequestOperationManager *request = [AFHTTPRequestOperationManager manager];
    request.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [request GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"------------%@", responseObject);
        
        self.detailDic = responseObject;
        self.notesImageArr = [responseObject objectForKey:@"days"];
        
        
        
        [self.notesTableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"错啦 000000======");
    }];

}

//游记详情
- (void)createTopimageOfNoteDetail {
    
    self.notesTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.notesTableView.delegate = self;
    self.notesTableView.dataSource = self;
    self.notesTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.notesTableView];
    
    //游记详情的顶部图
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 667 * 260)];
    //headerView.backgroundColor = [UIColor yellowColor];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:headerView.bounds];
    [imageView setImageWithURLStr:_imageString];
    [headerView addSubview:imageView];
    self.notesTableView.tableHeaderView = headerView;
    
    //游记详情顶部图上的头像
    UIImageView *userImage = [[UIImageView alloc] initWithFrame:CGRectMake(20, 120, 40, 40)];
    userImage.layer.cornerRadius = 20;
    userImage.clipsToBounds = YES;
    //userImage.backgroundColor = [UIColor yellowColor];
    [userImage setImageWithURLStr:_userImageString];
    [imageView addSubview:userImage];
    
    //游记详情顶部图上的标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 120, self.view.bounds.size.width - 100, 50 * self.view.bounds.size.height / 667)];
    //titleLabel.backgroundColor = [UIColor redColor];
    titleLabel.shadowOffset = CGSizeMake(2, 2);
    titleLabel.shadowColor = [UIColor blackColor];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.text = _titleOfTop;
    [imageView addSubview:titleLabel];
    
    
    [self.notesTableView registerClass:[NotesDetailTableViewCell class] forCellReuseIdentifier:@"reuse"];
    [self createHeaderView];
    [self dataHandle];
    [_notesTableView release];

    

    
    

    
    
    
    
    
    
}

// 创建游记顶部图
- (void)createHeaderView
{
    self.headerBackground = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.notesTableView.tableHeaderView.frame.size.width, self.notesTableView.frame.size.height / 667 * 260)];
    [self.notesTableView addSubview:self.headerBackground];
    [_notesTableView release];
    
    self.authorImage = [[UIImageView alloc]initWithFrame:CGRectMake(20 * self.view.frame.size.width / 375, 180 * self.view.frame.size.height / 667, 30, 30)];
    self.authorImage.layer.masksToBounds = YES;
    self.authorImage.layer.cornerRadius = 15;
    // 设置头像图片的点击事件
    self.authorImage.userInteractionEnabled = YES;
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoTappedAction:)];
    [self.authorImage addGestureRecognizer:singleTap];
    [self.notesTableView.tableHeaderView addSubview:self.authorImage];
    [_authorImage release];
    
    
    
}







//作者图片的点击事件
- (void)photoTappedAction:(UITapGestureRecognizer *)pta
{
    UserViewController *userVC = [[UserViewController alloc] init];
    NSString *id = [self.travelNotesDetail.user objectForKey:@"id"];
    userVC.userId = id;
    [self.navigationController pushViewController:userVC animated:YES];
    [userVC release];
    
}

- (void)creatNotesDetails
{
    
    
    
    
}

//一共有几个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.notesImageArr.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *str = [NSString stringWithFormat:@"第%@天", [[self.notesImageArr objectAtIndex:section] objectForKey:@"day"]];
    return str;
}


#pragma 协议必须实现的两个方法
//每个分区有几个cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //先确定是哪个分区, waypoints里有几个数组就有几个cell
    return [[[self.notesImageArr objectAtIndex:section] objectForKey:@"waypoints"] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NotesDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
      // cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (cell == nil) {
        cell = [[NotesDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"];
    }
   
//    [cell.notesDetailImages setImageWithURL:[NSURL URLWithString:[[self.notesImageArr[indexPath.section][@"waypoints"] objectAtIndex:indexPath.row] objectForKey:@"photo_1600"]]];
    
   
    [cell.notesDetailImages setImageWithURL:[NSURL URLWithString:[[[[self.notesImageArr objectAtIndex:indexPath.section] objectForKey:@"waypoints"] objectAtIndex:indexPath.row] objectForKey:@"photo_1600"]]];
    
    cell.notesDetail.text = [[[[self.notesImageArr objectAtIndex:indexPath.section] objectForKey:@"waypoints"] objectAtIndex:indexPath.row] objectForKey:@"text"];
    
    
    
    //cell.textLabel.text = [self.notesImageArr objectAtIndex:]
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    //自定义tableViewCell高度
    NSString *text = [[[[self.notesImageArr objectAtIndex:indexPath.section] objectForKey:@"waypoints"] objectAtIndex:indexPath.row] objectForKey:@"text"];
    
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:20]};
    CGRect rect = [text boundingRectWithSize:CGSizeMake(self.notesTableView.frame.size.width, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    
    return rect.size.height + ((self.notesTableView.bounds.size.width - 40) / 5 * 4+20+20) + 1;
    
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    self.navigationItem.title = self.detailTitle;
    
    [self createTopimageOfNoteDetail];
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
